﻿using ContributionDeposit.CoreFrontEnd;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using Models.DataModel;
using System.Data;
using Newtonsoft.Json;
using System.IO;

using Models.DataStore;
namespace ContributionDeposit.Controllers
{
    public class ContributionRateController : Core_FrontEnd
    {
        //
        // GET: /ContributionRate/
        public ActionResult Index(int? page, string searchtext = "")
        {
            ViewBag.SearchText = searchtext;
            ViewBag.ListContributionRate = fun_db.GetListContributionRate(searchtext);
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(fun_db.GetListContributionRate(searchtext).ToPagedList(pageNumber, pageSize));
        }
        public JsonResult GetAll(int? page, string searchtext = "")
        {
            ViewBag.SearchText = searchtext;
            ViewBag.ListContributionRate = fun_db.GetListContributionRate(searchtext);
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            var result = fun_db.GetListContributionRate(searchtext).ToPagedList(pageNumber, pageSize);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public JsonResult NameFund()
        {
            List<ContributionRateExtra> list = fun_db.GetListContributionRate();
            List<string> result = new List<string>();
            foreach (ContributionRateExtra c in list)
                result.Add(c.FundCode);

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        //
        // GET: /ContributionRate/Create
        public ActionResult Create()
        {
            ViewBag.ListFund = new SelectList(fun_db.GetListFund().ToList(), "FundId", "Code");
            ViewBag.ListPort = new SelectList(fun_db.GetlstPort().ToList(), "PortId", "Name");
            return View();
        }
        //
        // POST: /ContributionRate/Create
        [HttpPost]
        public ActionResult Create(ContributionRate contributionRate)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    string error= fun_db.InsertContributionRate(contributionRate);
                       if(error==null)
                           return RedirectToAction("Index");
                       else
                           return View(error);
                }
                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                return View(e.ToString());
            }
        }

        /// <summary>
        /// Get port Number
        /// </summary>
        /// <param name="PortId"></param>
        /// <returns></returns>
        public ActionResult GetValueFromDDL(Guid PortId)
        {
            var portnumber = fun_db.GetPortNumberById(PortId);
            return Json(portnumber, JsonRequestBehavior.AllowGet);
        }
        //
        // GET: /ContributionRate/Edit/5
        public ActionResult Edit(Guid id)
        {
            if (id == null)
                return View();
            ViewBag.ListFund = new SelectList(fun_db.GetListFund().ToList(), "FundId", "Code");
            ViewBag.ListPort = new SelectList(fun_db.GetlstPort().ToList(), "PortId", "Name");
            return View(fun_db.GetContributionRateById(id));
        }
        //
        // POST: /ContributionRate/Edit/5
        [HttpPost]
        public ActionResult Edit(ContributionRate contributionrate)
        {
            try
            {
                fun_db.UpdateContributionRate(contributionrate);
                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                return View(e.ToString());
            }
        }
        //
        // POST: /ContributionRate/Delete/5
        [HttpPost]
        public JsonResult Delete(Guid id)
        {
            var result=fun_db.DeleteContributionRate(id);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult DeleteSelectedContributionRate(IEnumerable<Guid> countryItemDelete)
        {
                var result = fun_db.DeleteSelectedContributionRate(countryItemDelete);
                return Json(result, JsonRequestBehavior.AllowGet);
        }


        public List<ContributionRateExtra> SearchText(string article)
        {
            var result = fun_db.GetListContributionRate(article);
            return result;
        }

        public ActionResult ExportContributionRate()
        {
            var fileName = string.Format("{0} {1}.xlsx", ControllerContext.RouteData.Values["controller"].ToString(), DateTime.Now.ToString("yyyyMMdd HHmmss"));

            string filepath = AppDomain.CurrentDomain.BaseDirectory + "App_Data/ExcelFiles/" + fileName;
            DataTable dt = Utilities.Utility.ConvertToDataTable(fun_db.GetListContributionRate().ToList());

            Utilities.Utility.WriteDataTableToExcel(dt, Path.GetFileNameWithoutExtension(fileName), filepath, "Details");
            byte[] filedata = System.IO.File.ReadAllBytes(filepath);
            string contentType = MimeMapping.GetMimeMapping(filepath);

            var cd = new System.Net.Mime.ContentDisposition
            {
                FileName = fileName,
                Inline = true,
            };

            Response.AppendHeader("Content-Disposition", cd.ToString());
            System.IO.File.Delete(filepath);
            return File(filedata, contentType);
        }
    }
}
